import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/model/get_chapter_model.dart';
import 'package:appflutterhomeschool/view_model/my_app_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ShowChapterDialogViewModel extends ChangeNotifier {
  BuildContext me;
  List<ChapteModel> titleContent;

  bool listBool = true;
  List<GetChapterModel> listGetChapterModel;
  List<Sections> sections = [];

  ShowChapterDialogViewModel(this.me, this.titleContent);

  initView() {
    MyAppViewModel myAppViewModel = Provider.of(me);
    DioUtils.getInstance().post(ApiService.getTextbookStructure, {
      "cls_mapping_id": myAppViewModel?.studentInformationModel?.classMappingId
    }, (successCallBack) {
      Map mapData = successCallBack["data"];
      if (mapData == null) {
        return;
      }
      if (mapData["data"] == null) {
        return;
      }
      List listData = mapData["data"];

      if (listData.length <= 0) {
        return;
      }
      listGetChapterModel = [];
      listData.forEach((element) {
        listGetChapterModel.add(GetChapterModel.fromJson(element));
      });
      notifyListeners();
    }, (errorCallBack) {});
  }

  chapterOnTap(int index) {
    if (listGetChapterModel[index].sections != null) {
      sections = listGetChapterModel[index].sections;
      if (sections.length <= 0) {
        Navigator.pop(me);
        return;
      }
      listBool = false;

      titleContent.first.order = listGetChapterModel[index].order;
      titleContent.first.chineseName = listGetChapterModel[index].chineseName;
      titleContent.first.select = false;
      titleContent.first.id = listGetChapterModel[index].id;
      titleContent.first.name = listGetChapterModel[index].name;

      titleContent.last.choice = true;
      titleContent.last.select = true;
      notifyListeners();
    }
  }

  sectionsOnTap(int index) {
    titleContent.last.order = listGetChapterModel[index].order;
    titleContent.last.chineseName = listGetChapterModel[index].chineseName;
    titleContent.last.id = listGetChapterModel[index].id;
    titleContent.last.select = false;
    titleContent.last.name = listGetChapterModel[index].name;
    Navigator.pop(me);
  }

  selectOnTap(int index) {
    if (index == 0) {
      listBool = true;
      titleContent.first.select = true;
      titleContent.last.select = false;
    } else {
      listBool = false;
      titleContent.first.select = false;
      titleContent.last.select = true;
    }
    notifyListeners();
  }
}
